//
// LockFileTest.m -- test out the lock file class
//		Written by Don Yacktman  Copyright (c) 1993 by Don Yacktman.
//				Version 1.0.  All rights reserved.
//
//		This notice may not be removed from this source code.
//
//	This program is included in the MiscKit by permission from the author
//	and its use is governed by the MiscKit license, found in the file
//	"LICENSE.rtf" in the MiscKit distribution.  Please refer to that file
//	for a list of all applicable permissions and restrictions.
//	

#import <misckit/misckit.h>
#import <appkit/appkit.h>
#import <stdio.h>

void main()
{
	id lockFile1 = [[MiscLockFile alloc] init];
	id lockFile2 = [[MiscLockFile alloc] init];

	printf("Standard lock/unlock test...");
	[lockFile1 setFileName:[[MiscString alloc] initString:"lock1"]];
	if (![lockFile1 lock]) { printf("failed on lock.\n"); exit(1); }
	printf("\n");
	system("ls -alsFg lock1");
	system("cat lock1"); printf("\n");
	if (![lockFile1 haveLock]) { printf("haveLock is wrong.\n"); exit(1); }
	if (![lockFile1 unlock]) { printf("failed on unlock.\n"); exit(1); }
	system("ls -alsFg lock1");
	printf("passed.\n");

	printf("Attempt to obtain lock when someone else has it...");
	[lockFile2 setFileName:[[MiscString alloc] initString:"lock1"]];
	if (![lockFile1 lock]) { printf("failed on initial lock.\n"); exit(1); }
	if ([lockFile2 lock]) { printf("second client got lock.\n"); exit(1); }
	if (![lockFile1 haveLock]) { printf("haveLock #1 is wrong.\n"); exit(1); }
	if ([lockFile2 haveLock]) { printf("haveLock #2 is wrong.\n"); exit(1); }
	if (![lockFile1 unlock]) { printf("failed on unlock.\n"); exit(1); }
	if ([lockFile2 unlock]) { printf("lock #2 unlocked.\n"); exit(1); }
	printf("passed.\n");

	printf("Dual lock/unlock test...");
	[lockFile1 setFileName:[[MiscString alloc] initString:"lock1"]];
	[lockFile2 setFileName:[[MiscString alloc] initString:"lock2"]];
	if (![lockFile1 lock]) { printf("failed on lock #1.\n"); exit(1); }
	if (![lockFile2 lock]) { printf("failed on lock #2.\n"); exit(1); }
	printf("\n");
	system("ls -alsFg lock1 lock2");
	system("cat lock1"); printf("\n");
	system("cat lock2"); printf("\n");
	if (![lockFile1 haveLock]) { printf("haveLock #1 is wrong.\n"); exit(1); }
	if (![lockFile2 haveLock]) { printf("haveLock #2 is wrong.\n"); exit(1); }
	if (![lockFile1 unlock]) { printf("failed on unlock #1.\n"); exit(1); }
	if (![lockFile2 unlock]) { printf("failed on unlock #2.\n"); exit(1); }
	system("ls -alsFg lock1 lock2");
	printf("passed.\n");

	exit(0);
}
